package com.sangu.dianxiu.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.sangu.dianxiu.BaseApp;
import com.sangu.dianxiu.ICallBack;
import com.sangu.dianxiu.R;
import com.sangu.dianxiu.adapter.OrderListAdapter;
import com.sangu.dianxiu.bean.order.OrderEntity;
import com.sangu.dianxiu.ui.OrderDetailActivity;
import com.sangu.dianxiu.utils.RepairOrderManager;
import com.sangu.dianxiu.utils.RepairTypeManager;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * A simple {@link Fragment} subclass.
 */
public class OrderFragment extends Fragment implements PullToRefreshBase.OnRefreshListener2<ListView>{
    private int start=0;        //从第0条开始加载订单数据
    private RepairOrderManager orderManager=new RepairOrderManager();       //使用此类进行订单数据的加载
    private OrderListAdapter orderListAdapter;                              //订单列表适配器
    private ArrayList<OrderEntity.OrdersEntity> orderList=new ArrayList<>();    //订单类
    @ViewInject(R.id.ptr_fragment_order_list)       //使用XUtils的注解框架，这样就不用每次都findViewById了
    private PullToRefreshListView ptrOrderList;     //订单列表应当使用刷新listview

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_order, container, false);
        ViewUtils.inject(this, view);           //若果没有此行代码，则注解不会生效
        initView();
        return view;
    }
    private void initView() {

        ptrOrderList.setMode(PullToRefreshBase.Mode.BOTH);
        ptrOrderList.setOnRefreshListener(this);
        orderListAdapter=new OrderListAdapter(orderList,getActivity());
        ptrOrderList.setAdapter(orderListAdapter);
        ptrOrderList.getRefreshableView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(), OrderDetailActivity.class);
                intent.putExtra("orderId",orderList.get(position-1).getOrderId());
                startActivity(intent);
            }
        });
        if(BaseApp.isLogin==false)
        {
            Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
        }else{
            //刚进入此界面就进行刷新，以显示订单
            ptrOrderList.setRefreshing();
        }
    }

    private void loadData() {
            HashMap<String,String> map=new HashMap<>();
            map.put("userId", BaseApp.user.getUserId()+"");
            map.put("start",start+"");
            map.put("count",10+"");     //每次请求加载十条数据
            start+=10;
            orderManager.getOrder(map, new ICallBack() {
                @Override
                public void onSuccess(String string) {
                    ptrOrderList.onRefreshComplete();
                    OrderEntity orderEntity= JSONObject.parseObject(string,OrderEntity.class);      //使用fastjson数据解析
                    if(orderEntity.getRc()==0)
                    {
                        orderList.addAll(orderEntity.getOrders());
                        orderListAdapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onError(VolleyError error) {

                }
            });

    }
    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {            //下拉刷新，重新加载数据
        orderList.clear();
        loadData();
    }
    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {              //上拉加载，但是没必要

    }

    //当Activity销毁的时候取消网络请求
    @Override
    public void onDestroy() {
        super.onDestroy();
        orderManager.requestQueue.cancelAll(RepairTypeManager.class);
    }
}
